package org.zanpu.timer.HttpClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zanpu.timer.HttpClient.vo.HttpVO;
import org.zanpu.timer.SpringRest.interf.HttpRestInf;

import java.io.IOException;

/**
 * 异步请求HTTP接口工具类
 * Created by MD on 2016/12/23.
 */
@Component
public class HTTPAsync {
    private final Logger logger = LoggerFactory.getLogger(HTTPAsync.class);
    @Autowired
    private HttpRestInf httpRest;

    public String SendRequest(final HttpVO httpVO) throws Exception{
        try{
            String result = "";
            logger.debug("HttpSend--[URL:{}----Variables{}]"+httpVO.getUrl(),httpVO.getUriVariables());
            if (httpVO.getMethod().equals("post"))
                result =  httpRest.post(httpVO.getUrl(), httpVO.getVariables());
            else if (httpVO.getMethod().equals("get"))
                result =  httpRest.get(httpVO.getUrl());
            logger.debug("HttpSend--[RESULT:{}]",result);
            return result;
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }

    }
}
